﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Text.RegularExpressions;
using MidwayBattle.Drawing;

namespace MidwayBattle
{
    /// <summary>
    /// Logique d'interaction pour WindowNewGame.xaml
    /// </summary>
    public partial class WindowNewGame : Window
    {
        private WindowLoadPlayer WLoad;
        private int selectedGameType;
        private int selectedRole;
        public  int Port;
        public string Ip;
        private MainWindow parent;

//-------------------------------------------------------------------------------------------------------------------
        public WindowNewGame(WindowLoadPlayer WLoad, MainWindow caller)
        {
            InitializeComponent();
            this.MinHeight = 400;
            this.MinWidth = 350;
            this.WLoad = WLoad;
            this.parent = caller;
            Drawings.SetBoatCursor(this);
        }

//-------------------------------------------------------------------------------------------------------------------
        private void CancelBtn_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

//-------------------------------------------------------------------------------------------------------------------
        private void StartBtn_Click(object sender, RoutedEventArgs e)
        {
            if (this.IJN_RadioButton.IsChecked == false && this.USN_RadioButton.IsChecked == false) // si pas de profil sélectionné
            {
                MessageBox.Show("You must choose a side", "New game", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            else
            {
                this.selectedGameType = this.GameType_CB.SelectedIndex;
                switch (selectedGameType)
                {
                    // si pas de type de jeu sélectionné
                    case -1:
                        {
                            MessageBox.Show("You must choose a game type", "New game", MessageBoxButton.OK, MessageBoxImage.Warning);
                            break;
                        }
                    // Si jeu contre humain ( = multijoueurs)
                    case 0:
                        {
                            selectedRole = this.MyRole.SelectedIndex;

                            //////////////////////////////////////////////////////////////////////////
                            switch (selectedRole)
                            {
                                // Si pas de rôle sélectionné
                                case -1:
                                    {
                                        MessageBox.Show("You must choose server OR client", "New game", MessageBoxButton.OK, MessageBoxImage.Warning);
                                        break;
                                    }

                                // Si hôte / serveur
                                case 0:
                                    {
                                        if (VerifPort())
                                        {
                                            parent.InitGame(CustomDefines.GAME_MULTIPLAYER, CustomDefines.MULTIPLAYER_HOST, Port, Ip);
                                            Close();
                                        }
                                        break;
                                    }


                                // Si client
                                case 1:
                                    {
                                        Ip = this.IP_Field.Text;
                                        if (Ip.Length > 0)
                                        {
                                            if (!Regex.IsMatch(Ip, @"^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$"))
                                            {
                                                MessageBox.Show("Your < IP > is not valid", "New game", MessageBoxButton.OK, MessageBoxImage.Warning);
                                            }
                                            else
                                            {
                                                if (VerifPort())
                                                {
                                                    parent.InitGame(CustomDefines.GAME_MULTIPLAYER, CustomDefines.MULTIPLAYER_CLIENT, Port, Ip);
                                                    Close();
                                                }
                                            }
                                        }
                                        else // Si pas d'IP entrée
                                        {
                                            MessageBox.Show("The IP is empty", "New game", MessageBoxButton.OK, MessageBoxImage.Warning);
                                        }
                                        break;
                                    }

                            } // END SWITCH ROLE
                            //////////////////////////////////////////////////////////////////////////
                            break;

                        } // END CASE 0 MULTIJOUEURS

                    // Si jeu single player
                    case 1:
                        {
                            parent.InitGame(CustomDefines.GAME_SINGLEPLAYER, -1, -1, "");
                            Close();
                            break;
                        }


                } // END SWITCH SELECTEDGAMETYPE


          }// END ELSE INITIAL

      } // END FONCTION STARTBTN_CLICK
//-------------------------------------------------------------------------------------------------------------------
        private void GameType_CB_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.selectedGameType = this.GameType_CB.SelectedIndex;
            if (selectedGameType == 0)
            {
                this.MyRole.Visibility = Visibility;
                this.IP_Field.Visibility = Visibility;
                this.Port_Field.Visibility = Visibility;
                this.label5.Visibility = Visibility;
                this.label6.Visibility = Visibility;
                this.label7.Visibility = Visibility;
                this.label9.Visibility = Visibility;
            }
            else
            {
                this.MyRole.SelectedIndex = -1;
                this.MyRole.Visibility = Visibility.Hidden;
                this.IP_Field.Clear();
                this.IP_Field.Visibility = Visibility.Hidden;
                this.Port_Field.Clear();
                this.Port_Field.Visibility = Visibility.Hidden;
                this.label5.Visibility = Visibility.Hidden;
                this.label6.Visibility = Visibility.Hidden;
                this.label7.Visibility = Visibility.Hidden;
                this.label9.Visibility = Visibility.Hidden;
            }
        }
//-------------------------------------------------------------------------------------------------------------------
        private Boolean VerifPort()
        {

            int tempPort;
            bool isNumeric = int.TryParse(Port_Field.Text, out tempPort);

            Boolean success = true;

            if (Port_Field.Text.Length == 0 || !isNumeric)
            {
                
                MessageBox.Show("The < Port > contains illegal characters, spaces, or nothing", "New game", MessageBoxButton.OK, MessageBoxImage.Warning);
                success = false;
            }
            else
            { 
                if(tempPort < 1 || tempPort > 65535)
                {
                    MessageBox.Show("The port number is incorrect: must be within the following range- 1 -> 65535", "New game", MessageBoxButton.OK, MessageBoxImage.Warning);
                    success = false;
                }
            }

            return success;

        }
//-------------------------------------------------------------------------------------------------------------------

        private void MyRole_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.selectedRole = this.MyRole.SelectedIndex;

            if (selectedRole == 0)
            {
                this.IP_Field.Clear();
                this.label6.Visibility = Visibility.Hidden;
                this.IP_Field.Visibility = Visibility.Hidden;
            }
            else
            {
                this.label6.Visibility = Visibility.Visible;
                this.IP_Field.Visibility = Visibility.Visible;
            }
        }
    }
}
